package src.main.java.com.pms.gui.dialog.sale;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import src.main.java.com.pms.service.SaleService;

public class SaleListDialog extends JDialog {
    private JTable saleTable;
    private DefaultTableModel tableModel;
    private SaleService saleService;
    private String searchName; // 用于存储搜索名称，以便刷新数据

    public SaleListDialog(JFrame parent) {
        super(parent, "寄卖列表", true);
        saleService = new SaleService();
        initComponents();
        loadSaleData();
    }

    public SaleListDialog(JFrame parent, String searchName) {
        super(parent, "搜索寄卖结果", true);
        saleService = new SaleService();
        this.searchName = searchName;
        initComponents();
        loadSaleData();
    }

    private void initComponents() {
        setSize(600, 400);
        setLocationRelativeTo(getParent());

        // 创建表格模型
        String[] columnNames = {"ID", "名称", "类型", "年龄", "期望价格", "卖家联系方式"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        saleTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(saleTable);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton refreshButton = new JButton("刷新");
        JButton cancelButton = new JButton("取消寄卖");
        JButton editButton = new JButton("编辑");

        refreshButton.addActionListener(e -> loadSaleData());
        cancelButton.addActionListener(e -> cancelSelectedSale());
        editButton.addActionListener(e -> editSelectedSale());

        buttonPanel.add(refreshButton);
        buttonPanel.add(cancelButton);
        buttonPanel.add(editButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void loadSaleData() {
        try {
            // 清空表格
            tableModel.setRowCount(0);

            // 获取寄卖数据
            ResultSet rs;
            if (searchName != null && !searchName.trim().isEmpty()) {
                rs = saleService.searchPetsForSale(searchName);
            } else {
                rs = saleService.viewPetsForSale();
            }

            while (rs.next()) {
                Object[] row = {
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("type"),
                        rs.getInt("age"),
                        rs.getDouble("asking_price"),
                        rs.getString("seller_contact")
                };
                tableModel.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载数据失败：" + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void cancelSelectedSale() {
        int selectedRow = saleTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要取消寄卖的宠物！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this,
                "确定要取消这只宠物的寄卖吗？", "确认取消寄卖",
                JOptionPane.YES_NO_OPTION);

        if (confirm == JOptionPane.YES_OPTION) {
            try {
                if (saleService.cancelPetSale(id)) {
                    JOptionPane.showMessageDialog(this, "寄卖取消成功！");
                    loadSaleData(); // 重新加载数据
                } else {
                    JOptionPane.showMessageDialog(this, "取消寄卖失败！",
                            "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "取消寄卖失败：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void editSelectedSale() {
        int selectedRow = saleTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要编辑的寄卖信息！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        double askingPrice = (Double) tableModel.getValueAt(selectedRow, 4);
        String sellerContact = (String) tableModel.getValueAt(selectedRow, 5);

        // 打开编辑对话框并传递当前数据
        new UpdateSalePriceDialog(this, id, askingPrice, sellerContact).setVisible(true);
        loadSaleData();
    }
}
